package com.ltmonitor.web.controller.adas;

import com.ltmonitor.adas.entity.AdasDeviceState;
import com.ltmonitor.adas.entity.AdasDeviceSystemInfo;
import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.MediaItem;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.IAdasDeviceStateService;
import com.ltmonitor.service.IAdasDeviceSystemInfoService;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.controller.command.TerminalCommandAction;
import com.ltmonitor.web.util.JsonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Adas外设系统信息和状态查询指令
 */
@Controller
@RequestMapping("/adasDeviceInfo")
public class AdasDeviceInfoAction extends TerminalCommandAction {

    @Autowired
    private IAdasDeviceSystemInfoService adasDeviceSystemInfoService;
    @Autowired
    private IAdasDeviceStateService adasDeviceStateService;


    /**
     * 打开查询外设状态页面
     * @param vehicleId
     * @param m
     * @return
     */
    @RequestMapping("/viewAdasDeviceState.action")
    public String viewAdasDeviceState(long vehicleId, Model m) {
        m.addAttribute("vehicleId", vehicleId);
        return "/adasDeviceInfo/adasDeviceState";
    }

    /**
     * 打开查询外设系统信息页面
     * @param vehicleId
     * @param m
     * @return
     */
    @RequestMapping("/viewAdasDeviceSystemInfo.action")
    public String viewAdasDeviceSystemInfo(long vehicleId, Model m) {
        m.addAttribute("vehicleId", vehicleId);
        return "/adasDeviceInfo/adasDeviceSystemInfo";
    }


    /**
     * 下发查询外设状态指令
     * @param vehicleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/sendQueryAdasDeviceStateCommand.action")
    public JsonMessage sendQueryAdasDeviceStateCommand(long vehicleId) {
        try {
            TerminalCommand tc = new TerminalCommand();
            tc.setVehicleId(vehicleId);
            tc.setCmdType(0x8900);
            tc.setSubCmdType(0xF7);

            StringBuilder cmdData = new StringBuilder();
            cmdData.append(0x64).append(";").append(0x65)
                    .append(";").append(0x66).append(";").append(0x67);
            tc.setCmdData(cmdData.toString());
            SendCommand(tc);

            return json(true, tc.getEntityId());
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex);
            return json(false, ex.getMessage());
        }
    }

    /**
     * 下发查询外设系统信息指令
     * @param vehicleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/sendQueryAdasDeviceSystemInfoCommand.action")
    public JsonMessage sendQueryAdasDeviceSystemInfoCommand(long vehicleId) {
        try {
            TerminalCommand tc = new TerminalCommand();
            tc.setVehicleId(vehicleId);
            tc.setCmdType(0x8900);
            tc.setSubCmdType(0xF8);
            StringBuilder cmdData = new StringBuilder();
            cmdData.append(0x64).append(";").append(0x65)
                    .append(";").append(0x66).append(";").append(0x67);
            tc.setCmdData(cmdData.toString());
            SendCommand(tc);

            return json(true, tc.getEntityId());
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex);
            return json(false, ex.getMessage());
        }
    }

    /**
     * 根据指令ID，获取终端返回的多媒体记录数据列表
     * @param commandId
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryAdasDeviceInfo.action")
    public JsonMessage queryAdasDeviceInfo(long commandId) {
        try {
            TerminalCommand tc = getTerminalCommand(commandId);
            BasicData bd = getBasicDataService().getBasicDataByCode(
                    tc.getStatus(), "TCommandStatus");
            String status = bd != null ? bd.getName() : "";
            Map result = new HashMap();
            result.put("status", status);
            if (TerminalCommand.STATUS_UPLOADED.equals(tc.getStatus())) {
                if(tc.getSubCmdType() == 0xF7)
                {
                    List<AdasDeviceState> data = this.adasDeviceStateService.getAdasDeviceStateByCommandId(commandId);
                    result.put("data", data);
                }else if(tc.getSubCmdType() == 0xF8)
                {
                    List<AdasDeviceSystemInfo> data = this.adasDeviceSystemInfoService.getAdasDeviceSystemInfoByCommandId(commandId);
                    result.put("data", data);
                }
                return json(true, result);
            } else if (TerminalCommand.STATUS_NEW.equals(tc.getStatus()) == false
                    && TerminalCommand.STATUS_PROCESSING.equals(tc.getStatus()) == false) {
                return json(true, status);
            } else {
                return json(false, status);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return json(false, "查询时发送错误:" + ex.getMessage());
        }
    }


}
